package groupb6.shopsystem.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import groupb6.shopsystem.pojo.entity.*;
import groupb6.shopsystem.pojo.form.*;
import groupb6.shopsystem.service.SellerService;
import groupb6.shopsystem.util.JWTUtil;
import groupb6.shopsystem.util.response.UniversalResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "2:卖家主页")
@ApiSupport(author = "蔡昌宏")
@RestController
@RequestMapping("/seller")
public class SellerController {

    @Resource
    private SellerService sellerService;

    @ApiOperation("上架商品")
    @PostMapping("/product/add")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 1002, message = "插入失败"),
            @ApiResponse(code = 7005, message = "商品信息不完整"),
            @ApiResponse(code = 7007, message = "商品信息类型不匹配"),
            @ApiResponse(code = 2004, message = "用户权限不足"),
    })
    @ResponseBody
    public UniversalResponse<Product> AddProduct(@RequestBody AddProductForm product) {
        User user = JWTUtil.getCurrentUser();
        // 创建一个新的 Product 对象，将前端传来的 AddProductForm 对象的数据复制给它
        Product product1 = new Product();
        product1.setName(product.getName());
        product1.setImagePath(product.getImagePath());
        product1.setPrice(product.getPrice());
        product1.setType(product.getType());
        product1.setSold(0);
        product1.setStock(product.getStock());
        product1.setDetail(product.getDetail());
        product1.setSellerId(user.getId());
        product1.setStatus(1);
        // 调用 sellerService.AddProduct 方法，传递前端传来的 Product 对象
        return sellerService.AddProduct(product1);
    }

    @ApiOperation("下架商品")
    @PostMapping("/product/unlist/{productId}")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 7002, message = "未找到商品信息"),
            @ApiResponse(code = 2004, message = "用户权限不足"),
    })
    @ResponseBody
    public UniversalResponse<Product> UnlistProduct(@PathVariable("productId") Integer productId) {
        return sellerService.UnlistProduct(productId);
    }



    @ApiOperation("显示商家所有商品信息")
    @GetMapping("/product/show")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 7003, message = "还未添加商品"),
    })
    @ResponseBody
    public UniversalResponse<List<Product>> showProductsBySellerId() {
        return sellerService.showProductsBySellerId();
    }

    @ApiOperation("显示商家订单列表")
    @GetMapping("/order/show")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 7004, message = "还未创建订单"),
    })
    @ResponseBody
    public UniversalResponse<List<OrderWithReturnInfo>> showOrdersBySellerId() {
        return sellerService.showOrdersBySellerId();
    }

    /**
     * 仅先实现发货功能
     * @return
     */
    @ApiOperation("商家发货")
    @PostMapping("/order/Ship/{orderId}")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 3000, message = "订单还未付款"),
            @ApiResponse(code = 3004, message = "订单已发货"),
            @ApiResponse(code = 2004, message = "用户权限不足"),
    })
    @ResponseBody
    public UniversalResponse<Order> ShipOrder(@RequestParam("orderId") Integer orderId)
    {
        return sellerService.ShipOrder(orderId);
    }

    @ApiOperation("下架时检查订单")
    @PostMapping("/product/check")
    @ApiResponses(value = {
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 7002, message = "商品可下架"),
    })
    @ResponseBody
    public UniversalResponse<List<Integer>> CheckorderByproductid(@RequestBody OrderProduct form) {
        Integer productId = form.getProductId();
        return sellerService.CheckorderByProductId(productId);
    }

    @ApiOperation("修改商品信息")
    @PostMapping("/product/modify")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 7005, message = "商品信息不完整"),
    })
    @ResponseBody
    public UniversalResponse<List<Product>> ModifyProduct(@RequestBody ModifyProductForm product) {
        // 创建一个新的 Product 对象，将前端传来的 AddProductForm 对象的数据复制给它
        Product product1 = new Product();
        product1.setId(product.getId());
        product1.setName(product.getName());
        product1.setImagePath(product.getImagePath());
        product1.setPrice(product.getPrice());
        product1.setType(product.getType());
        product1.setStock(product.getStock());
        product1.setDetail(product.getDetail());
        return sellerService.ModifyProduct(product1);
    }

    @ApiOperation("获取商品原有属性信息")
    @GetMapping("/product/attributes")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 7002, message = "未找到商品信息"),
    })
    @ResponseBody
    public UniversalResponse<ModifyProductForm> getProductAttributes(@RequestParam Integer productId){
        return sellerService.getProductAttributes(productId);
    }

    @ApiOperation("获取商家信息")
    @GetMapping("/search/information")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "用户不存在"),
            @ApiResponse(code = 7002, message = "获取商家信息失败"),
    })
    @ResponseBody
    public UniversalResponse<SearchSellerForm> getSellerInformation(@RequestParam Integer Id){
        return sellerService.getSellerInformation(Id);
    }

//    @ApiOperation("显示退货列表")
//    @GetMapping("/order/return")
//    @ApiResponses(value = {
//            @ApiResponse(code = 1000, message = "成功"),
//            @ApiResponse(code = 1001, message = "失败"),
//            @ApiResponse(code = 7001, message = "尚未生成订单"),
//            @ApiResponse(code = 7002, message = "已全部退货成功"),
//            @ApiResponse(code = 7003, message = "尚未生成退货订单"),
//    })
//    @ResponseBody
//    public UniversalResponse<List<List<Return>>> showReturn(){
//        return sellerService.showReturn();
//    }

    @ApiOperation("是否同意退货  |  卖家收货")
    @PostMapping("/AgreeReturn/agree")
    @ApiResponses(value = {
            @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 1001, message = "订单状态不匹配"),
    })
    @ResponseBody
    UniversalResponse<Return> AgreeReturnById(@RequestBody HandleReturnForm handle) {
        return sellerService.AgreeReturnById(handle);
    }

//    @ApiOperation("卖家收货")
//    @PostMapping("/ReceiveReturn/agree")
//    @ApiResponses(value = {
//            @ApiResponse(code = 1000, message = "成功"),
//            @ApiResponse(code = 1001, message = "失败"),
//    })
//    @ResponseBody
//    UniversalResponse<Return> ReceiveReturnById(@RequestBody HandleReturnForm handle) {
//        return sellerService.ReceiveReturnById(handle);
//    }
}
